package club.calong.sanniu.controller;

import club.calong.sanniu.http.response.Response;
import club.calong.sanniu.pojo.Permission;
import club.calong.sanniu.service.PermissionService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/permission")
public class PermissionController {

	private final PermissionService service;

	public PermissionController(PermissionService service) {
		this.service = service;
	}

	@PostMapping()
	public Response create(@RequestBody Permission permission) {

		permission.setId(null);
		if (service.insert(permission))
			return new Response(Response.SUCCESS, Response.Message.INSERT_OK, permission);
		else
			return new Response(Response.FAILURE, Response.Message.INSERT_ERROR, permission);
	}

	@DeleteMapping("{id}")
	public Response delete(@PathVariable("id") Integer id) {

		if (service.delete(id))
			return new Response(Response.SUCCESS, Response.Message.DELETE_OK, null);
		else
			return new Response(Response.FAILURE, Response.Message.DELETE_ERROR, null);
	}

	@PutMapping()
	public Response update(@RequestBody Permission permission) {

		if (service.update(permission))
			return new Response(Response.SUCCESS, Response.Message.UPDATE_OK, null);
		else
			return new Response(Response.FAILURE, Response.Message.UPDATE_ERROR, null);
	}

	@GetMapping("{id}")
	public Response select(@PathVariable("id") int id) {

		return new Response(Response.SUCCESS, null, service.select(id));
	}

	@GetMapping
	public Response list() {
		return new Response(Response.SUCCESS, null, service.list(null, null));
	}

	@GetMapping("list/{page}/{size}")
	public Response list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {

		return new Response(Response.SUCCESS, null, service.list(page, size));
	}
}
